﻿using Bricks.Business.Service;
using Bricks.Component.Utils.Const;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using Bricks.Component.Utils.Extensions;
using Bricks.Site.Models;
using System.Text;
using Bricks.Business.Service.CommonSupport;
using Bricks.Component.Utils;
using Bricks.Site.Web.CommonUtils;
using System.Xml;

namespace Bricks.Site.Web.Controllers
{
    [Export]
    public class ProphaseFileInfoController : BaseController
    {
        [Import]
        public IProjectStageInfoService ProjectStageInfoService { get; set; }
        [Import]
        public IProphaseFileInfoService ProphaseFileInfoService { get; set; }
        [Import]
        public IProphaseFileModifyInfoService ProphaseFileModifyInfoService { get; set; }

        [Import]
        public IDocumentService DocumentService { get; set; }

        public ActionResult ProphaseFileList()
        {
            ViewBag.ToolBarInfo = base.GetToolBarInfo();
            return View();
        }

        //private void ConstructTools()
        //{
        //    List<UIFunctionView> menuActions = base.GetUserMenuActions("ProphaseFileList");
        //    StringBuilder sb = new StringBuilder();
        //    foreach (UIFunctionView item in menuActions)
        //    {
        //        string buttonStr = "<a href='javascript:void(0)' id='btn" + item.ActionCode + "' class='easyui-linkbutton' onclick='" + item.MenuCode + "." + item.ActionCode + "()' data-options='plain:true,iconCls:\"" + item.Icon + "\"'>" + item.ActionName + "</a>";
        //        sb.Append(buttonStr);
        //    }

        //    ViewBag.ToolBarInfo = sb.ToString();
        //}

        public ActionResult GetProphaseFileList(string pid)
        {
            Guid userID = Guid.NewGuid();
            if (SystemData.Instance.CurrentUser != null)
            {
                userID = SystemData.Instance.CurrentUser.ID;
            }
            return Json(ProphaseFileInfoService.GetProphaseFileInfoList(pid.ToGuid(), userID), JsonRequestBehavior.AllowGet);
        }



        public ActionResult GetProjectStageInfo(string id, string pid)
        {
            Guid guidID;
            if (!String.IsNullOrEmpty(id))
            {
                guidID = id.ToGuid();
            }
            else
            {
                guidID = Guid.NewGuid();
            }

            ProjectStageInfoView model = ProjectStageInfoService.GetProjectStageInfo(guidID);
            if (model == null)
            {
                model = new ProjectStageInfoView();
                model.ID = guidID;
                model.PID = pid.ToGuid();
                model.DealPerson = SystemData.Instance.CurrentUser.ID;
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult SaveProjectStageInfo(ProjectStageInfoView model)
        {
            return Json(ProjectStageInfoService.SaveProjectStageInfo(model), "text/html");
        }

        public ActionResult DeleteProjectStageInfo(string id)
        {
            Guid guidID = id.ToGuid();
            var exeResult = ProjectStageInfoService.Delete(guidID);
            return Json(exeResult);
        }

        [HttpPost]
        public ActionResult SetStageOrder(string id, string order)
        {
            Guid guidID = id.ToGuid();
            var exeResult = ProjectStageInfoService.SetOrder(guidID, order);
            return Json(exeResult);
        }


        public ActionResult GetProphaseFileInfo(string id, string pid, string projectStageID)
        {
            Guid guidID;
            if (!String.IsNullOrEmpty(id))
            {
                guidID = id.ToGuid();
            }
            else
            {
                guidID = Guid.NewGuid();
            }

            ProphaseFileInfoView model = ProphaseFileInfoService.GetProphaseFileInfo(guidID);
            if (model == null)
            {
                model = new ProphaseFileInfoView();
                model.ID = guidID;
                model.ProjectID = pid.ToGuid();
                if (!string.IsNullOrEmpty(projectStageID))
                {
                    model.ProjectStageID = projectStageID.ToGuid();
                }
                model.DealTime = DateTime.Now;
                model.DealPerson = SystemData.Instance.CurrentUser.ID.ToString();
            }
            return View(model);
        }

        public ActionResult GetProjectStageInfoList(string pid)
        {
            return Json(ProjectStageInfoService.GetProjectStageInfoList(pid.ToGuid()), "text/html");
        }

        public ActionResult GetProphaseFileModifyInfoList(string pid)
        {
            return Json(ProphaseFileModifyInfoService.GetProphaseFileModifyInfoList(pid.ToGuid()), "text/html");
        }

        [HttpPost]
        public ActionResult SaveProphaseFileInfo(ProphaseFileInfoView model)
        {
            OperationResult result = null;
            try
            {
                ProphaseFileInfoService.SaveProphaseFileInfo(model);

                List<ProphaseFileModifyInfoView> lstProphaseFileModifyInfoView = new List<ProphaseFileModifyInfoView>();
                if (model.JsonProphaseFileModifyInfo != "[]")
                {
                    string jsonProphaseFileModifyInfo = "{\"lstProphaseFileModifyInfoView\":" + model.JsonProphaseFileModifyInfo + "}";
                    XmlNode prophaseFileModifyInfoNode = XMLJsonUtils.JsonToXml(jsonProphaseFileModifyInfo).SelectSingleNode("root/lstProphaseFileModifyInfoView");
                    foreach (XmlNode childNode in prophaseFileModifyInfoNode.ChildNodes)
                    {
                        ProphaseFileModifyInfoView add = new ProphaseFileModifyInfoView();
                        XmlNode node = childNode.SelectSingleNode("ID");
                        if (node == null || string.IsNullOrEmpty(node.InnerText))
                        {
                            add.ID = GuidHelper.NewGuid();
                        }
                        else
                        {
                            add.ID = childNode.SelectSingleNode("ID").InnerText.ToGuid();
                        }
                        add.PID = childNode.SelectSingleNode("PID").InnerText.ToGuid();

                        add.MidifyTime = childNode.SelectSingleNode("MidifyTime").InnerText;
                        add.MidifyReason = childNode.SelectSingleNode("MidifyReason").InnerText;
                        lstProphaseFileModifyInfoView.Add(add);
                    }
                }
                ProphaseFileModifyInfoService.SaveProphaseFileModifyInfo(model.ID, lstProphaseFileModifyInfoView);

                result = new OperationResult(OperationResultType.Success, "操作成功");
            }
            catch (Exception ex)
            {
                result = new OperationResult(OperationResultType.Error, ex.Message);
            }
            return Json(result, "text/html");
        }

        public ActionResult DeleteProphaseFileInfo(string id)
        {
            Guid guidID = id.ToGuid();
            var exeResult = ProphaseFileInfoService.Delete(guidID);
            if (exeResult.ResultType == OperationResultType.Success)
            {
                var docDeleteResult = DocumentService.DeleteByInstanceID(guidID);
                if (docDeleteResult == true)
                {
                    exeResult.Message += "，已删除关联附件记录";
                }
            }
            return Json(exeResult);
        }

        [HttpPost]
        public ActionResult SetFileOrder(string id, string projectStageID, string order)
        {
            Guid guidID = id.ToGuid();
            Guid guidProjectStageID = projectStageID.ToGuid();
            var exeResult = ProphaseFileInfoService.SetOrder(guidID, guidProjectStageID, order);
            return Json(exeResult);
        }


        public ActionResult PreviewProphaseFileInfo(string id)
        {
            ProphaseFileInfoView model = ProphaseFileInfoService.GetProphaseFileInfo(id.ToGuid());
            return View(model);
        }
    }
}
